package com.maixy.rabbitmqdemo.delay;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName DelaySender
 * @Description 生产者
 * @Date 2021/3/23 20:54
 * @Author maixy
 */
@Component
public class DelaySender {

    @Resource
    private RabbitTemplate rabbitTemplate;

    public void sendMsg() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("消息发送时间:"+sdf.format(new Date()));
        rabbitTemplate.convertAndSend("delay_exchange_1", "delay_queue_1", "hello i am delay msg", new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setHeader("x-delay",10000);
                return message;
            }
        });
    }
}
